package com.zhang.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zhang.gulimall.product.entity.AttrEntity;
import com.zhang.gulimall.product.service.CategoryService;
import com.zhang.gulimall.product.vo.AttrGroupVo;
import com.zhang.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhang.gulimall.product.entity.AttrGroupEntity;
import com.zhang.gulimall.product.service.AttrGroupService;
import com.zhang.gulimall.common.utils.PageUtils;
import com.zhang.gulimall.common.utils.R;



/**
 * 属性分组
 *
 * @author zhang
 * @email sunlightcs@gmail.com
 * @date 2022-09-14 10:23:17
 */
@RestController
    @RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 关联列表
     */
    @RequestMapping("/{groupId}/attr/relation")
    public R relationList(@RequestParam Map<String, Object> params, @PathVariable("groupId")String groupId){
        List<AttrEntity> list = attrGroupService.queryRelationPage(params, groupId);

        return R.ok().put("data", list);
    }

    /**
     * 非关联列表
     */
    @RequestMapping("/{groupId}/noattr/relation")
    public R noRelationList(@RequestParam Map<String, Object> params, @PathVariable("groupId")String groupId){
        List<AttrEntity> list = attrGroupService.queryNoRelationPage(params, groupId);

        return R.ok().put("page", list);
    }

    /**
     * 新增关联
     */
    @RequestMapping("/attr/relation")
    public R save(@RequestBody List<AttrVo> attrVos){
        attrGroupService.saveRelations(attrVos);

        return R.ok();
    }

    /**
     * 非关联列表
     */
    @RequestMapping("/{catelogId}/withattr")
    public R groupWithAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId")String catelogId){
        List<AttrGroupVo> list = attrGroupService.groupWithAttrList(params, catelogId);

        return R.ok().put("data", list);
    }

    /**
     * 删除
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrVo> attrVos){
        attrGroupService.removeRelation(attrVos);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId")String catId){
        PageUtils page = attrGroupService.queryPage(params, catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCategoryPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
